package com.zkyc.framework.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkyc.framework.common.constant.NumberConstant;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.mapper.MediaTagGroupMapper;
import com.zkyc.framework.common.mapper.MediaTagMapper;
import com.zkyc.framework.common.pojo.*;
import com.zkyc.framework.common.utils.WebsiteUtil;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.mapper.*;
import com.zkyc.framework.website.service.MediaTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;

/**
 * @author bing.bai
 * @create 2022/6/15
 */
@Service
public class MediaTagServiceImpl implements MediaTagService {

    @Autowired
    private MediaTagGroupMapper mediaTagGroupMapper;

    @Autowired
    private MediaTagMapper mediaTagMapper;

    @Autowired
    SystemFileRefTagMapper systemFileRefTagMapper;

    @Autowired
    SystemFileRefSubjectMapper systemFileRefSubjectMapper;

    @Autowired
    ContentMapper contentMapper;

    @Autowired
    private ColumnDataRefMediaTagMapper dataRefMediaTagMapper;

    @Autowired
    private ColumnDataMapper columnDataMapper;

    @Override
    public ResultVO queryList() {
        List<MediaTagGroup> mediaTagGroupList = mediaTagGroupMapper.getMediaTagGroupList(WebsiteUtil.getWebsiteId());
        return ResultVO.data(mediaTagGroupList);
    }

    @Override
    public ResultVO saveGroup(MediaTagGroup mediaTagGroup) {
        if (checkGroup(mediaTagGroup)) return ResultVO.fail(ResultCode.TAGGROUP_DUPLICATION);
        String websiteId = WebsiteUtil.getWebsiteId();
        if (websiteId != null) {
            mediaTagGroup.setWebsiteId(websiteId);
        }
        mediaTagGroupMapper.insert(mediaTagGroup);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO updateGroup(MediaTagGroup mediaTagGroup) {
        if (checkGroup(mediaTagGroup)) return ResultVO.fail(ResultCode.TAGGROUP_DUPLICATION);
        mediaTagGroupMapper.updateById(mediaTagGroup);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    private boolean checkGroup(MediaTagGroup mediaTagGroup) {
        QueryWrapper<MediaTagGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_group_name", mediaTagGroup.getMediaGroupName());
        String websiteId = WebsiteUtil.getWebsiteId();
        if (websiteId != null) {
            queryWrapper.eq("website_id", websiteId);
        }
        List<MediaTagGroup> mediaTagGroups = mediaTagGroupMapper.selectList(queryWrapper);
        if (mediaTagGroups.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public ResultVO deleteGroup(MediaTagGroup mediaTagGroup) {
        mediaTagGroupMapper.deleteById(mediaTagGroup.getId());
        LambdaQueryWrapper<MediaTag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(MediaTag::getTagGroupId,mediaTagGroup.getId());
        // 删除之前先查询
        List<MediaTag> mediaTagList = mediaTagMapper.selectList(lqw);
        mediaTagMapper.delete(lqw);
        List<Integer> mediaTagIdList = mediaTagList.stream().map(MediaTag::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(mediaTagIdList)) {
            LambdaQueryWrapper<ColumnDataRefMediaTag> dataRefMediaTagLqw = new LambdaQueryWrapper<>();
            dataRefMediaTagLqw.in(ColumnDataRefMediaTag::getMediaTagId, mediaTagIdList);
            dataRefMediaTagMapper.delete(dataRefMediaTagLqw);
        }
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO saveTag(MediaTag mediaTag) {
        if (checkTag(mediaTag)) return ResultVO.fail(ResultCode.TAG_DUPLICATION);
        mediaTagMapper.insert(mediaTag);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO updateTag(MediaTag mediaTag) {
        if (checkTag(mediaTag)) return ResultVO.fail(ResultCode.TAG_DUPLICATION);
        mediaTagMapper.updateById(mediaTag);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    private boolean checkTag(MediaTag mediaTag) {
        QueryWrapper<MediaTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_group_id", mediaTag.getTagGroupId());
        queryWrapper.eq("media_tag_name", mediaTag.getMediaTagName());
        List<MediaTag> mediaTags = mediaTagMapper.selectList(queryWrapper);
        if (mediaTags.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public ResultVO deleteTag(MediaTag mediaTag) {
        Integer mediaTagId = mediaTag.getId();
        mediaTagMapper.deleteById(mediaTagId);
        //查询该标签下的所有文件
        QueryWrapper<SystemFileRefTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("media_tag_id", mediaTagId);
        List<SystemFileRefTag> systemFileRefTags = systemFileRefTagMapper.selectList(queryWrapper);
        List<Long> fileIds = new ArrayList();
        for (SystemFileRefTag systemFileRefTag : systemFileRefTags) {
            fileIds.add(systemFileRefTag.getFileId());
        }
        //删除该标签
        systemFileRefTagMapper.deleteByTagId(mediaTagId);
        //查看文件是否有标签
        fileIds.forEach(fileId -> {
            QueryWrapper<SystemFileRefTag> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("file_id", fileId);
            List<SystemFileRefTag> fileRefTags = systemFileRefTagMapper.selectList(queryWrapper1);
            if (fileRefTags.size() == 0) {
                //没有标签的话就添加空标签
                int tagId = mediaTagMapper.selectZero();
                SystemFileRefTag fileRefTag = new SystemFileRefTag();
                fileRefTag.setFileId(fileId);
                fileRefTag.setMediaTagId(tagId);
                systemFileRefTagMapper.insert(fileRefTag);
            }
        });
        // todo 删除所有稿件对该标签的引用
        LambdaQueryWrapper<ColumnDataRefMediaTag> dataRefMediaTagLqw = new LambdaQueryWrapper<>();
        dataRefMediaTagLqw.eq(ColumnDataRefMediaTag::getMediaTagId, mediaTagId);
        dataRefMediaTagMapper.delete(dataRefMediaTagLqw);
        return ResultVO.success(ResultCode.SUCCESS);
    }

    @Override
    public ResultVO queryContentTag(String columnId) {
        List<MediaTag> mediaTags = contentMapper.queryTagByColumnId(columnId, null);
        return ResultVO.data(mediaTags);
    }

    @Override
    public ResultVO queryListByColumnId(Integer columnId) {
        // 查数据id
        LambdaQueryWrapper<ColumnData> columnDataLqw = new LambdaQueryWrapper<>();
        columnDataLqw.select(ColumnData::getId);
        columnDataLqw.eq(ColumnData::getColumnId, columnId).eq(ColumnData::getStatus, NumberConstant.NUMBER_1);
        List<ColumnData> columnDataList = columnDataMapper.selectList(columnDataLqw);
        if (CollectionUtils.isEmpty(columnDataList)) {
            return ResultVO.data(Collections.EMPTY_LIST);
        }
        List<Integer> columnDataIdList = columnDataList.stream().map(ColumnData::getId).collect(Collectors.toList());
        // 查询中间表
        LambdaQueryWrapper<ColumnDataRefMediaTag> dataRefMediaTagLqw = new LambdaQueryWrapper<>();
        dataRefMediaTagLqw.select(ColumnDataRefMediaTag::getMediaTagId);
        dataRefMediaTagLqw.in(ColumnDataRefMediaTag::getColumnDataId, columnDataIdList);
        List<ColumnDataRefMediaTag> dataRefMediaTagList = dataRefMediaTagMapper.selectList(dataRefMediaTagLqw);
        if (CollectionUtils.isEmpty(dataRefMediaTagList)) {
            // 为空则不需要继续
            return ResultVO.data(Collections.EMPTY_LIST);
        }
        // 需要去重
        List<Integer> mediaTagIdList = dataRefMediaTagList.stream().map(ColumnDataRefMediaTag::getMediaTagId).distinct().collect(Collectors.toList());
        // 查询标签表
        LambdaQueryWrapper<MediaTag> mediaTagLqw = new LambdaQueryWrapper<>();
        mediaTagLqw.select(MediaTag::getId, MediaTag::getMediaTagName);
        mediaTagLqw.in(MediaTag::getId, mediaTagIdList);
        List<MediaTag> mediaTagList = mediaTagMapper.selectList(mediaTagLqw);
        return ResultVO.data(mediaTagList);
    }
}
